'''
* This is the projet for Brtc LlmOps Platform
* @Author Leon-liao <liaosiliang@alltman.com>
* @Description //TODO 
* @File: auth_handler.py
* @Time: 2025/10/11
* @All Rights Reserve By Brtc
'''
from dataclasses import dataclass
from flask_login import login_required, logout_user
from huggingface_hub import logout
from injector import inject

from internal.schema.auth_schema import PasswordLoginReq, PasswordLoginResp
from internal.service import AccountService
from pkg.response import success_json, success_message, validate_error_json


@inject
@dataclass
class AuthHandler:
    """LLMOps 平台自由授权认证处理"""
    account_service:AccountService


    def password_login(self):
        """账号密码登录"""
        #1、提取并校验数据
        req = PasswordLoginReq()

        if not req.validate:
            return validate_error_json(req.errors)

        #2、调用服务登陆账号
        credential = self.account_service.password_login(req.email.data, req.password.data)

        #3、创建响应节后并返回
        resp = PasswordLoginResp()
        return success_json(resp.dump(credential))



    @login_required
    def logout(self):
        """推出登录，思路是直接清除凭证"""
        logout_user()
        return success_message("退出登录成功！")